<?php
/**
 * bay3ValidatorLostPassword Validator
 * @author Redouane
 * @since  20/04/2011
 */
class bay3ValidatorLostPassword extends sfValidatorBase
{
  public function configure($options = array(), $messages = array())
  {
    $this->addOption('email_field','email');
    $this->addOption('password_field');
    $this->addOption('firstname_field');
  }

  protected function doClean($values)
  {
    $email = isset($values[$this->getOption('email_field')]) ? $values[$this->getOption('email_field')] : '';
    
    // Email is set ?
    if (empty($email)) return false;
    
    // seller exists ?
    $seller = Doctrine_Core::getTable("Seller")->findOneByEmail($email);
    if ($seller instanceof Seller){
        $values = array_merge($values, array('password_field' => $seller->password));
        $values = array_merge($values, array('firstname_field' => $seller->firstname));
        return  $values;
    }else{
        throw new sfValidatorErrorSchema($this, array($this->getOption('email_field') => new sfValidatorError($this, 'invalid')));
    }	
  }
}
